package com.dm.library.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.dm.library.R;


/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  选择 拍照 相片
 * Created by tangjian on 2016/11/22.
 */

public class SelectPhotoDialog extends Dialog implements View.OnClickListener {
    private View view;
    private OnViewClickListener mOnViewClickListener;
    private final TextView tabTop;
    private final TextView tabBottom;

    public void setOnViewClickListener(OnViewClickListener onViewClickListener) {
        this.mOnViewClickListener = onViewClickListener;
    }

    @SuppressWarnings("deprecation")
    public SelectPhotoDialog(Activity context) {
        super(context, R.style.customDialog);
        view = LayoutInflater.from(context).inflate(R.layout.select_photo_dialog, null);
        tabTop = (TextView) view.findViewById(R.id.tab_top);
        tabBottom = (TextView) view.findViewById(R.id.tab_bottom);
        tabTop.setOnClickListener(this);
        tabBottom.setOnClickListener(this);
        view.findViewById(R.id.tab_cancel).setOnClickListener(this);
        this.setContentView(view);
        this.setCancelable(true);
        view.findViewById(R.id.popup_window_layout).setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                dismiss();
                return false;
            }
        });

        this.setContentView(view);
        Window dialogWindow = this.getWindow();
        if (null != dialogWindow) {
            dialogWindow.setGravity(Gravity.BOTTOM);
            dialogWindow.setWindowAnimations(R.style.AnimBottom);

            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = context.getWindowManager().getDefaultDisplay().getWidth();// 宽度全屏
            dialogWindow.setAttributes(lp);
        }
    }

    public void setTopString(String topString) {
        if (null != tabTop && null != topString) {
            tabTop.setText(topString);
        }
    }

    public void seBottomString(String bottomString) {
        if (null != tabBottom && null != bottomString) {
            tabBottom.setText(bottomString);
        }
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tab_top) {
            dismiss();
            if (null != mOnViewClickListener) {
                mOnViewClickListener.onSelectPhotoClick();
            }
        } else if (view.getId() == R.id.tab_bottom) {
            dismiss();
            if (null != mOnViewClickListener) {
                mOnViewClickListener.onTakePhotoClick();
            }
        } else if (view.getId() == R.id.tab_cancel) {
            dismiss();
        }

    }

    public interface OnViewClickListener {
        /**
         * 从相册选择
         */
        void onSelectPhotoClick();

        /**
         * 拍照
         */
        void onTakePhotoClick();
    }
}
